package com.promotores;

import com.promotores.R;
import com.promotores.util.Parametros;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class ActivityIngresarParametros extends Activity implements OnClickListener{
	EditText txtAsistMin;
	EditText txtAsistMax;
	EditText txtPrecMin;
	EditText txtPrecMax;
	String strAsistMin;
	String strAsistMax;
	String strPrecMin;
	String strPrecMax;
	float ftAsistMin;
	float ftAsistMax;
	float ftPrecMin;
	float ftPrecMax;
	String strIdEvento;
	String nombEvento;
	String mailPromotor;
	/**
	 * @see android.app.Activity#onCreate(Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.pr_ingresarparametros);
		strIdEvento = this.getIntent().getStringExtra("idEvento");
		nombEvento = this.getIntent().getStringExtra("nombEvento");
		mailPromotor = this.getIntent().getStringExtra("mailPromotor");
		
		TextView txtTitulo = new TextView(this); 
		txtTitulo = (TextView)findViewById(R.id.detalleEvento);
		txtTitulo.setText("Evento: "+nombEvento);
		txtAsistMin = (EditText)findViewById(R.id.txtAsistMin);
		txtAsistMax = (EditText)findViewById(R.id.txtAsistMax);
		txtPrecMin = (EditText)findViewById(R.id.txtPrecMin);
		txtPrecMax = (EditText)findViewById(R.id.txtPrecMax);
		
//		strAsistMin = txtAsistMin.getText().toString();
//		strAsistMax = txtAsistMax.getText().toString();
//		strPrecMin = txtPrecMin.getText().toString();
//		strPrecMax = txtPrecMax.getText().toString();
		ImageButton btnPreviousCostoFijo = (ImageButton) findViewById(R.id.btnPreviousCostoFijo);
		btnPreviousCostoFijo.setOnClickListener(this);
		ImageButton btnNextCostoFijo = (ImageButton) findViewById(R.id.btnNextCostoFijo);
		btnNextCostoFijo.setOnClickListener(this);
		ImageButton btnPreviousCostoUnit = (ImageButton) findViewById(R.id.btnPreviousCostoUnit);
		btnPreviousCostoUnit.setOnClickListener(this);
		ImageButton btnNextCostoUnit = (ImageButton) findViewById(R.id.btnNextCostoUnit);
		btnNextCostoUnit.setOnClickListener(this);
		ImageButton btnPreviousPrecioMin = (ImageButton) findViewById(R.id.btnPreviousPrecioMin);
		btnPreviousPrecioMin.setOnClickListener(this);
		ImageButton btnNextPrecioMin = (ImageButton) findViewById(R.id.btnNextPrecioMin);
		btnNextPrecioMin.setOnClickListener(this);
		ImageButton btnPreviousPrecioMax = (ImageButton) findViewById(R.id.btnPreviousPrecioMax);
		btnPreviousPrecioMax.setOnClickListener(this);
		ImageButton btnNextPrecioMax = (ImageButton) findViewById(R.id.btnNextPrecioMax);
		btnNextPrecioMax.setOnClickListener(this);
		
		txtAsistMin.setText(String.valueOf(100000));
		txtAsistMax.setText(String.valueOf(50));
		txtPrecMin.setText(String.valueOf(1.0));
		txtPrecMax.setText(String.valueOf(2.0));
		
		ImageButton offline = (ImageButton)findViewById(R.id.imgOfflineMode);
        offline.setOnClickListener(this);
        if (Parametros.isOffline_mode()){
        	offline.setVisibility(View.VISIBLE);
        }
		
		Button boton = (Button) findViewById(R.id.btnLimpiar);
		boton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				//TextView txtAsistMin = (TextView) findViewById(R.id.txtAsistMin);
				txtAsistMin.setText("");
				//TextView txtAsistMax = (TextView) findViewById(R.id.txtAsistMax);
				txtAsistMax.setText("");
				//TextView txtPrecMin = (TextView) findViewById(R.id.txtPrecMin);
				txtPrecMin.setText("");
				//TextView txtPrecMax = (TextView) findViewById(R.id.txtPrecMax);
				txtPrecMax.setText("");
			}
		});
		
		boton = (Button) findViewById(R.id.btnGenerar);
		boton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				strAsistMin = txtAsistMin.getText().toString();
				strAsistMax = txtAsistMax.getText().toString();
				strPrecMin = txtPrecMin.getText().toString();
				strPrecMax = txtPrecMax.getText().toString();
				
				if (strAsistMin==null || strAsistMax==null || strPrecMin==null || strPrecMax==null){
					Toast.makeText(getApplicationContext(), "Debe completar todos los campos", Toast.LENGTH_LONG).show();
					return;
				}
				else if (strAsistMin.compareTo("")==0 || strAsistMax.compareTo("")==0 
						|| strPrecMin.compareTo("")==0 || strPrecMax.compareTo("")==0){
					Toast.makeText(getApplicationContext(), "Debe completar todos los campos", Toast.LENGTH_LONG).show();
					return;
				}
				
				Float precMax = (float)0;
				Float precMin = (float)0;
					
				try{
					precMax = Float.parseFloat(strPrecMax);
					precMin = Float.parseFloat(strPrecMin);
					
					if (precMin>=precMax){
						Toast.makeText(getApplicationContext(), "El rango del factor de precio es inconsistente", Toast.LENGTH_LONG).show();
						return;
					}
									
					Intent estimar = new Intent(ActivityIngresarParametros.this, ActivityVerEstimacion.class);
					estimar.putExtra("CostoVarUnit", strAsistMax);
					estimar.putExtra("CostoFijo", strAsistMin);
					estimar.putExtra("PrecMax", strPrecMax);
					estimar.putExtra("PrecMin", strPrecMin);
					estimar.putExtra("idEvento",strIdEvento);
					estimar.putExtra("nombEvento",nombEvento);
					estimar.putExtra("mailPromotor", mailPromotor);
					startActivity(estimar);
					
				}
				catch(Exception e){
					Toast.makeText(getApplicationContext(), "Data inconsistente", Toast.LENGTH_LONG).show();
					return;
				}			
				
			}
		});
	}
	@Override
	public void onClick(View v) {
		strAsistMin = txtAsistMin.getText().toString();
		strAsistMax = txtAsistMax.getText().toString();
		strPrecMin = txtPrecMin.getText().toString();
		strPrecMax = txtPrecMax.getText().toString();
		
		try {
		if (strAsistMin.compareTo("") != 0){
		if (v.getId()==R.id.btnPreviousCostoFijo){
			if (Float.parseFloat(txtAsistMin.getText().toString()) > 0) {
			ftAsistMin = Float.parseFloat(txtAsistMin.getText().toString()) - 10000;
			txtAsistMin.setText(String.valueOf(ftAsistMin));
			}
		}
		if (v.getId()==R.id.btnNextCostoFijo){
			ftAsistMin = Float.parseFloat(txtAsistMin.getText().toString()) + 10000;
			txtAsistMin.setText(String.valueOf(ftAsistMin));
		}
		}
		
		if (strAsistMax.compareTo("") != 0){
		if (v.getId()==R.id.btnPreviousCostoUnit){
			if (Float.parseFloat(txtAsistMax.getText().toString()) > 0) {
			ftAsistMax =  Float.parseFloat(txtAsistMax.getText().toString()) - 10;
			txtAsistMax.setText(String.valueOf(ftAsistMax));
			}
		}
		if (v.getId()==R.id.btnNextCostoUnit){
			ftAsistMax =  Float.parseFloat(txtAsistMax.getText().toString()) + 10;
			txtAsistMax.setText(String.valueOf(ftAsistMax));
		}
		}
		
		if (strPrecMin.compareTo("") != 0){
		if (v.getId()==R.id.btnPreviousPrecioMin){
			if (Float.parseFloat(txtPrecMin.getText().toString()) > 0) {
			ftPrecMin =  (float) (Float.parseFloat(txtPrecMin.getText().toString()) - 0.1);
			txtPrecMin.setText(String.valueOf(ftPrecMin));
			}
		}
		if (v.getId()==R.id.btnNextPrecioMin){
			ftPrecMin =  (float) (Float.parseFloat(txtPrecMin.getText().toString()) + 0.1);
			txtPrecMin.setText(String.valueOf(ftPrecMin));
		}
		}
		
		if (strPrecMax.compareTo("") != 0){
		if (v.getId()==R.id.btnPreviousPrecioMax){
			if (Float.parseFloat(txtPrecMax.getText().toString()) > 0) {
			ftPrecMax = (float) (Float.parseFloat(txtPrecMax.getText().toString()) - 0.1);
			txtPrecMax.setText(String.valueOf(ftPrecMax));
			}
		}
		if (v.getId()==R.id.btnNextPrecioMax){
			ftPrecMax = (float) (Float.parseFloat(txtPrecMax.getText().toString()) + 0.1);
			txtPrecMax.setText(String.valueOf(ftPrecMax));
		}
		}
		
		}
		catch (Exception e){
			Toast.makeText(getApplicationContext(), "Data inconsistente", Toast.LENGTH_LONG).show();
			return;
		}

	}
}
